//
//  QZHSocial.m
//  Analytics
//
//  Created by 米翊米 on 2018/7/9.
//  Copyright © 2018年 米翊米. All rights reserved.
//

#import "QZHSocial.h"

#import <UMCommon/UMCommon.h>
#import <UMAnalytics/MobClick.h>
#import <UMShare/UMShare.h>
#import <UMShare/UMSocialManager.h>

@implementation QZHSocial

///初始化社会化组件所需信息,(如三方登陆，分享，统计)
+(void)initAppkey:(NSString *)key channel:(NSString *)channel {
    [UMConfigure initWithAppkey:key channel:channel];
    
#ifdef DEBUG
    [UMConfigure setLogEnabled:YES];
#else
    [UMConfigure setLogEnabled:NO];
#endif
}

///设置第三方平台信息
+(void)initSocialInfo:(void (^)(NSMutableArray<QZHSocialConfig *> *infos))config {
    if (!config) {
        return;
    }
    NSMutableArray *infos = [NSMutableArray array];
    config(infos);
    for (QZHSocialConfig *info in infos) {
        NSInteger platform = info.platform;
        [[UMSocialManager defaultManager] setPlaform:platform appKey:info.appKey appSecret:info.appSecret redirectURL:info.urlString];
    }
}

///获取授权信息
+ (void)socialUserInfoPlatform:(QZHSocialPlatformType)platformtype reslut:(void(^)(QZHSocialUserInfo *userInfo))resultBlock {
    if (!resultBlock) {
        return;
    }
    NSInteger platform = platformtype;
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:platform currentViewController:nil completion:^(id result, NSError *error) {
        if (!error) {
            QZHSocialUserInfo *userInfo = [[QZHSocialUserInfo alloc] init];
            UMSocialUserInfoResponse *response = (UMSocialUserInfoResponse *)result;
            userInfo.openid = response.openid;
            userInfo.unionid = response.unionId;
            userInfo.nickName = response.name;
            userInfo.gender = response.gender;
            userInfo.iconurl = response.iconurl;
            
            resultBlock(userInfo);
        } else {
            NSLog(@"获取授权信息失败: %@", error.description);
        }
    }];
}

///分享
+(void)socialShareInfo:(void(^)(QZHSocialShareInfo *info))makeInfo result:(void(^)(BOOL result))resultBlock {
    if (!makeInfo) {
        return;
    }
    QZHSocialShareInfo *shareInfo = [[QZHSocialShareInfo alloc] init];
    makeInfo(shareInfo);
    
    NSInteger platform = shareInfo.platform;
    UMSocialMessageObject *object = [[UMSocialMessageObject alloc] init];
    object.title = shareInfo.title;
    object.text = shareInfo.content;
    if (shareInfo.link.length > 0) {
        UMShareWebpageObject *webObject = [UMShareWebpageObject shareObjectWithTitle:shareInfo.title descr:shareInfo.content thumImage:shareInfo.images.firstObject];
        webObject.webpageUrl = shareInfo.link;
        object.shareObject = webObject;
    } else if (shareInfo.images.count > 0) {
        UMShareImageObject *imageObject =  [UMShareImageObject shareObjectWithTitle:shareInfo.title descr:shareInfo.content thumImage:shareInfo.images.firstObject];
        imageObject.shareImageArray = shareInfo.images;
        object.shareObject = imageObject;
    }
    [[UMSocialManager defaultManager] shareToPlatform:platform messageObject:object currentViewController:nil completion:^(id result, NSError *error) {
        if (resultBlock) {
            NSLog(@"share error: %@", error.description);
            if (error) {
                resultBlock(NO);
            } else {
                resultBlock(YES);
            }
        }
    }];
}

@end
